﻿using System.Data.Entity;
using OpusSuite.Models.Model.WebAdmin;
using OpusSuite.Models.Model.Ref;
using OpusSuite.Models.Model.Shared;
using OpusSuite.Utility;
using System.Data.Common;
using System.Data.Entity.Infrastructure;

using OpusSuite.Models.Model.LegalEntity.Owner;
using OpusSuite.Models.Model.LegalEntity.Agent;
using OpusSuite.Models.Model.Property;
using OpusSuite.Models.Model.Supplier;

namespace OpusSuite.Models.EFModel
{
    public class EfModelContext : DbContext
    {
        public DbSet<ModelMeta> ModelMeta { get; set; }

        public DbSet<User> Users { get; set; }
        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<UserSession> UserSessions { get; set; }
        public DbSet<UserSessionHistory> UserSessionHistory { get; set; }
        public DbSet<UserProfileSetting> UserProfileSettings { get; set; }

        public DbSet<Country> Countries { get; set; }
        public DbSet<Language> Languages { get; set; }
        public DbSet<MoneyFormat> MoneyFormats { get; set; }
        public DbSet<DateFormat> DateFormats { get; set; }
        public DbSet<Theme> Themes { get; set; }

        public DbSet<Client> Clients { get; set; }
        public DbSet<Owner> Owners { get; set; }
        public DbSet<Agent> Agents { get; set; }
        public DbSet<CommercialProperty> CommercialProperties { get; set; }
        public DbSet<ResidentialProperty> ResidentialProperties { get; set; }

        public DbSet<Supplier> Suppliers { get; set; }

        public DbSet<ApplicationMessage> ApplicationMessages { get; set; }

        public EfModelContext(DbConnection connection)
            : base(connection, false)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

            base.OnModelCreating(modelBuilder);
        }

        public void CreateDatabase(string dbUserName, string dbPassword, string dbDatabase, string dbServer, Enums.ServerTypes serverTypes)
        {
            if (Database.Exists())
                Database.Delete();

            Database.Create();
            Database.Initialize(true);
        }
    }
}
